package com.c6906.vdam.das.schedule;

import com.c6906.vdam.das.config.DasOpenStatus;
import com.c6906.vdam.das.service.AlarmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@EnableConfigurationProperties({DasOpenStatus.class})
public class TerminalOffline {
    private static final Logger logger = LoggerFactory.getLogger(TerminalOffline.class);

    @Resource
    DasOpenStatus openStatus;
    @Resource
    AlarmService alarmService;

    /**
     * 每两分钟根据LastGps表生成和更新终端离线异常表
     */
//    @Scheduled(fixedDelay = 120000)
    public void analysisOffline() {
        if (openStatus.isOpenStatus() && openStatus.isOpenLastGpsToRedis()) {
            logger.info("【终端异常】开始执行定时任务......");
            alarmService.analyzeOfflineVehicles();
            logger.info("【终端异常】结束执行定时任务......");
        }
    }

    /**
     * 更新因为车辆变更等各种情况，不需要监管的车辆的离线异常，使和终端监控查询的结果保持一致
     */
//    @Scheduled(cron = "0 0 22 * * *")
    public void updateErrorChanged() {
        if (openStatus.isOpenStatus() && openStatus.isOpenLastGpsToRedis()) {
            logger.info("【终端异常清除】开始执行定时任务......");
            alarmService.endErrors();
            logger.info("【终端异常清除】结束执行定时任务......");
        }
    }
}
